package com.solo.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.solo.common.core.base.service.impl.BasicServiceImpl;
import com.solo.common.core.utils.collection.CollUtils;
import com.solo.system.api.entity.SysUserRole;
import com.solo.system.mapper.SysRoleMapper;
import com.solo.system.mapper.SysUserMapper;
import com.solo.system.mapper.SysUserRoleMapper;
import com.solo.system.model.user.SysUserRoleConvert;
import com.solo.system.model.user.req.AllocatedUserReq;
import com.solo.system.model.user.req.AssignRoleReq;
import com.solo.system.model.user.req.AssignUserReq;
import com.solo.system.model.user.req.UnallocatedUserReq;
import com.solo.system.model.user.resp.AllocatedUserResp;
import com.solo.system.model.user.resp.UnallocatedUserResp;
import com.solo.system.model.user.resp.UserRoleResp;
import com.solo.system.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 用户角色关联 Service实现类
 * @author 十一
 * @since 2024-10-16 17:18
 * 人生若只如初见，何事秋风悲画扇
 **/
@Service
@RequiredArgsConstructor
public class SysUserRoleServiceImpl extends BasicServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    private final SysUserMapper sysUserMapper;
    private final SysRoleMapper sysRoleMapper;
    private final SysUserRoleMapper sysUserRoleMapper;
    
    @Override
    public UserRoleResp queryUserRole(Long userId) {
        // 获取全部的角色信息
        List<UserRoleResp.RoleInfo> roleInfos = QueryChain.of(this.sysRoleMapper)
                .listAs(UserRoleResp.RoleInfo.class);
        
        // 获取用户下所属的角色id集合
        List<Long> roleIds = sysUserRoleMapper.selectRoleIdsByUserId(userId);
        
        // 构造用户角色信息
        UserRoleResp userRoleResp = new UserRoleResp();
        userRoleResp.setRoleInfos(roleInfos);
        userRoleResp.setSelectIds(roleIds);
        return userRoleResp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean assignRole(AssignRoleReq req) {
        // 删除旧的用户角色关联
        sysUserRoleMapper.deleteByUserIds(Collections.singleton(req.getUserId()));

        Set<Long> roleIds = req.getRoleIds();
        if (CollUtils.isEmpty(roleIds)) {
            return Boolean.TRUE;
        }
        // 保存新的用户角色关联
        return saveBatch(SysUserRoleConvert.INSTANCE.convert(req));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean assignUser(AssignUserReq req) {
        // 删除旧的用户角色关联
        sysUserRoleMapper.deleteByRoleIds(Collections.singleton(req.getRoleId()));
        
        Set<Long> ids = req.getUserIds();
        if (CollUtils.isEmpty(ids)) {
            return Boolean.TRUE;
        }
        // 批量保存新的用户角色关联
        return saveBatch(SysUserRoleConvert.INSTANCE.convert(req));
    }

    @Override
    public Page<AllocatedUserResp> queryAllocatedUserPage(Page<AllocatedUserResp> page, AllocatedUserReq req) {
        return sysUserMapper.queryAllocatedUserPage(page, req);
    }

    @Override
    public Page<UnallocatedUserResp> queryUnallocatedUserPage(Page<UnallocatedUserResp> page, UnallocatedUserReq req) {
        return sysUserMapper.queryUnallocatedUserPage(page, req);
    }

}
